package olivetv

import (
	"io"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/go-olive/olive/foundation/olivetv/util"
)

const (
	KsLiveDetailQuery = `
	query LiveDetail($principalId: String) {  
		liveDetail(principalId: $principalId) {  
			liveStream
		}
	}
	`
	KsUserInfoQuery = `
	query userInfoQuery($principalId: String) {  
		userInfo(principalId: $principalId) {  
			name
			living
		}
	}
	`
)

func init() {
	registerSite("kuaishou", &kuaishou{})
}

type kuaishou struct {
	base
}

func (this *kuaishou) Name() string {
	return "快手"
}

func (this *kuaishou) Snap(tv *TV) (err error) {
	tv.Info = &Info{
		Timestamp: time.Now().Unix(),
	}
	return this.setV2(tv)
}

// func (this *kuaishou) setV1(tv *TV) error {
// 	if err := this.setRoomOnV1(tv); err != nil {
// 		return err
// 	}
// 	return this.setStreamURLV1(tv)
// }

// func (this *kuaishou) setStreamURLV1(tv *TV) error {
// 	if tv.cookie == "" {
// 		return ErrCookieNotSet
// 	}
// 	// if !tv.roomOn {
// 	// 	return nil
// 	// }
// 	ksAG := new(model.KsLiveDetailAutoGenerated)
// 	req := &util.HttpRequest{
// 		URL:    "https://live.kuaishou.com/graphql",
// 		Method: "POST",
// 		RequestData: map[string]interface{}{
// 			"operationName": "LiveDetail",
// 			"variables": map[string]string{
// 				"principalId": tv.RoomID,
// 			},
// 			"query": KsLiveDetailQuery,
// 		},
// 		ResponseData: ksAG,
// 		ContentType:  "application/json",
// 		Header: map[string]string{
// 			"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38",
// 			"referer":    "https://live.kuaishou.com/",
// 			"cookie":     tv.cookie,
// 		},
// 	}
// 	if err := req.Send(); err != nil {
// 		return err
// 	}

// 	if len(ksAG.Data.WebLiveDetail.LiveStream.PlayUrls) > 0 {
// 		tv.roomOn = true
// 		tv.streamURL = ksAG.Data.WebLiveDetail.LiveStream.PlayUrls[0].URL
// 	}
// 	tv.roomName = ksAG.Data.WebLiveDetail.LiveStream.Caption

// 	return nil
// }

// func (this *kuaishou) setRoomOnV1(tv *TV) error {
// 	if tv.cookie == "" {
// 		return ErrCookieNotSet
// 	}
// 	ksAG := new(model.KsUserInfoAutoGenerated)
// 	req := &util.HttpRequest{
// 		URL:    "https://live.kuaishou.com/graphql",
// 		Method: "POST",
// 		RequestData: map[string]interface{}{
// 			"operationName": "userInfoQuery",
// 			"variables": map[string]string{
// 				"principalId": tv.RoomID,
// 			},
// 			"query": KsUserInfoQuery,
// 		},
// 		ResponseData: ksAG,
// 		ContentType:  "application/json",
// 		Header: map[string]string{
// 			"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38",
// 			"referer":    "https://live.kuaishou.com/",
// 			"cookie":     tv.cookie,
// 		},
// 	}
// 	if err := req.Send(); err != nil {
// 		return err
// 	}

// 	tv.roomOn = ksAG.Data.UserInfo.Living
// 	tv.streamerName = ksAG.Data.UserInfo.Name

// 	return nil
// }

func (this *kuaishou) setV2(tv *TV) error {
	if tv.cookie == "" {
		return ErrCookieNotSet
	}
	req, err := http.NewRequest("GET", "https://live.kuaishou.com/profile/"+tv.RoomID, nil)
	if err != nil {
		return err
	}
	req.Header.Add("cookie", tv.cookie)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	content := string(body)

	if !strings.Contains(content, "直播中") {
		return nil
	}

	tv.streamerName, _ = util.Match(`title="([^"]+)" target="_blank"`, content)
	tv.roomName, _ = util.Match(`title="([^"]+)" class="router-link-exact-active`, content)
	tv.streamURL, _ = util.Match(`"url":"([^"]+)"`, content)
	tv.streamURL, _ = strconv.Unquote(`"` + tv.streamURL + `"`)
	if tv.streamURL != "" {
		tv.roomOn = true
	}

	return nil
}
